package com.yehuo.app.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.yehuo.app.model.Result;

import java.util.*;

/**
 * API工具类
 */
public class APiUtil {

//    private final static String URL_PREFIX = "http://103.100.159.117:18080/";
    private final static String URL_PREFIX = "http://109.206.246.92:18080/";

    private final static String SECRET_KEY = "123456";

    public static Result getData(String url, Map<String, Object> parameter) {
        String nonce = RandomUtil.randomNumbers(5);
        long timestamp = System.currentTimeMillis();
        String sign = SecureUtil.sha1(nonce + "|" + SECRET_KEY + "|" + timestamp);
        System.out.println(sign);
        String result = HttpUtil.createPost(URL_PREFIX + url)
                .header("nonce", nonce)
                .header("timestamp", String.valueOf(timestamp))
                .header("sign", sign)
                .header("Content-Type", "application/json")
                .body(new Gson().toJson(parameter))
                .execute().body();
        Result R = new Gson().fromJson(result, Result.class);
        return R;
    }

    /**
     * 动态获取表名
     *
     * @param year
     * @param month
     * @return
     */
    public static String getTableName(String tableName, int year, int month) {
        Date startTime = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        int x = month - 1 + (year % 3) * 12;
        return tableName.concat("_").concat(String.valueOf(x));
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        //发送者用户ID
        map.put("sender", "_C_b_bcc");
        //群发目标ID
        List<String> users = new ArrayList<>();
        users.add("_C_b_bcc");
        map.put("targets", users);
        Map<String, Object> payload = new HashMap<>();
        payload.put("type", 1);
        payload.put("searchableContent","我是QQ机器人小冰");
        map.put("payload", payload);
        Result result = APiUtil.getData("admin/message/multicast", map);
        System.out.println(result);
    }

}
